<?php

namespace app\admin\controller;

use app\admin\model\DormitoryBuilding;
use app\common\controller\Backend;
use app\common\library\Client;
use app\common\model\School;
use app\common\model\Subject;

class Common extends Backend
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 学校列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 15:46
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function schools()
    {
        /*$result = Client::instance()->schools();
        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['id'], 'name' => $value['name']];
        }
        $this->success('', null, $data);*/

        $result = School::whereIn('id', $this->auth->getSchoolIds())
            ->field('id,pid,name,province_id,city_id,district_id,address,logo,code,note')
            ->select();
        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['id'], 'name' => $value['name']];
        }
        $this->success('', null, $data);
    }

    /**
     * 年级列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 15:46
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function grades()
    {
        $result = Client::instance()->grades();

        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['id'], 'name' => $value['name']];
        }

        $this->success('', null, $data);
    }

    /**
     * 班级列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/20
     * @Time 16:03
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function classes()
    {
        $school_id = $this->request->request('school_id');
        $grade_id = $this->request->request('grade_id');
        $result = Client::instance()->classes($school_id, $grade_id);

        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['id'], 'name' => $value['name']];
        }

        $this->success('', null, $data);
    }

    public function subjects()
    {
        $school_id = $this->request->request('school_id', 0);
        $data = Subject::where('school_id', $school_id)->select();
        $this->success('', null, $data);
    }

    /**
     * 园区列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/23
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function gardens()
    {
        $school_id = $this->request->request('school_id');
        $result = Client::instance()->gardens($school_id);

        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['number'], 'name' => $value['name'], 'id' => $value['id']];
        }

        $this->success('', null, $data);
    }

    /**
     * 楼栋列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/23
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildings()
    {
        $garden_id = $this->request->request('garden_id');
        $result = Client::instance()->buildings($garden_id);

        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['number'], 'name' => $value['name'], 'id' => $value['id']];
        }

        $this->success('', null, $data);
    }

    /**
     * 楼栋列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/23
     *
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function buildingsByNumber()
    {
        $school_id = $this->request->request('school_id');
        $garden_number = $this->request->request('garden_number');
        $result = Client::instance()->buildingsByNumber($school_id, $garden_number);

        $data = [];
        foreach ($result as $key => $value) {
            $data[] = ['value' => $value['number'], 'name' => $value['name'], 'id' => $value['id']];
        }

        $this->success('', null, $data);
    }

    /**
     * 根据楼栋id获取楼层数
     */
    public function floorCount()
    {
        $building_id = $this->request->request('building_id');

        $result = Client::instance()->building($building_id);

        if (!$result) {
            $this->error('您选择的楼栋不存在', null, []);
        }

        $building = DormitoryBuilding::get($result['id']);

        if (!$building) {
            $this->error('请先补全楼栋信息', null, []);
        }

        $data['floor_count'] = $result['floor'];
        $data['dorm_bed_count'] = $building['dorm_bed_count'];
        $data['dorm_price'] = $building['dorm_price'];

        $this->success('', null, $data);
    }

}